/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotEmpty;

import com.eelpo.common.util.BlankUtils;

/**
 * <pre>
 * 企业结构实体，用于映射T_TREE_STRUCT表(Persist Object)
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Entity
@Table(name = "T_ENTERPRISE_RELATION")
public class TEnterpriseRelation implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 7925499662388819211L;

	/**
	 * 结构Id
	 */
	@NotEmpty
	private String id;

	/**
	 * 深度
	 */
	@NotNull
	private Integer depth;

	/**
	 * 父实体
	 */
	@NotNull
	private TEnterprise parentEnterprise;

	/**
	 * 子实体
	 */
	@NotNull
	private TEnterprise childEnterprise;

	public TEnterpriseRelation() {

	}

	public TEnterpriseRelation(String id) {
		this.id = id;
	}

	public TEnterpriseRelation(TEnterprise parentEnterprise, TEnterprise childEnterprise, Integer depth) {
		this.depth = depth;
		this.childEnterprise = childEnterprise;
		this.parentEnterprise = parentEnterprise;
	}

	@Id
	@Column(length = 36)
	@GenericGenerator(name = "uuid", strategy = "uuid2")
	@GeneratedValue(generator = "uuid")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(nullable = false)
	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parentId", nullable = false)
	public TEnterprise getParentEnterprise() {
		return parentEnterprise;
	}

	public void setParentEnterprise(TEnterprise parentEnterprise) {
		this.parentEnterprise = parentEnterprise;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "childId", nullable = false)
	public TEnterprise getChildEnterprise() {
		return childEnterprise;
	}

	public void setChildEnterprise(TEnterprise childEnterprise) {
		this.childEnterprise = childEnterprise;
	}

	@Override
	public boolean equals(Object obj) {
		if (BlankUtils.isBlank(obj)) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		if (!(obj instanceof TEnterpriseRelation)) {
			return false;
		}

		TEnterpriseRelation relation = (TEnterpriseRelation) obj;

		if (BlankUtils.isBlank(this.id) || BlankUtils.isBlank(relation.getId())) {
			return false;
		}

		return this.id.equals(relation.getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return "TEnterpriseRelation[id=" + id + "]";
	}

}